package com.fable.ntzw_ga.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fable.common.constants.CommonConstant;
import com.fable.common.vo.ResultVO;
import com.fable.ntzw_ga.entity.FeastRecompense;
import com.fable.ntzw_ga.service.IFeastRecompenseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Created by gzh 2023-03-17 03:12:54
 */
@Api(tags = "回礼记录")
@RestController
@RequestMapping("/ntzw_ga/t-recompense")
@PreAuthorize("hasRole('admin')")
public class FeastRecompenseController {


    @Autowired
    IFeastRecompenseService recompenseService;

    @ApiOperation(value = "回礼记录列表")
    @PostMapping("/list")
    public ResultVO getDataList(@RequestBody Map<String, Object> map) {
        LambdaQueryWrapper<FeastRecompense> wrapper = new LambdaQueryWrapper<>();
        Map<String, String> stringMap = (Map<String, String>) map.get("filters");
        FeastRecompense recompense = JSON.parseObject(JSON.toJSONString(map.get("filters")), FeastRecompense.class);
        String filed = stringMap.get("filed");
        String orderBy = stringMap.get("orderBy");
        if (StringUtils.isNotBlank(recompense.getName()) || StringUtils.isNotBlank(recompense.getAddress())) {
            wrapper.like(FeastRecompense::getName, recompense.getName()).or().like(FeastRecompense::getAddress, recompense.getAddress());
        }
        if (StringUtils.isNotBlank(filed) && StringUtils.isNotBlank(orderBy)) {
            wrapper.last("order by " + filed + StrUtil.SPACE + orderBy + StrUtil.SPACE + CommonConstant.LIMIT);
        } else {
            wrapper.orderByDesc(FeastRecompense::getCreateTime).last(CommonConstant.LIMIT);
        }
//        PageInfo<FeastRecompense> guestPageInfo = PageInfoUtils.getPageInfo(Convert.toInt(map.get("pageNum")), Convert.toInt(map.get("pageSize")), recompenseService.list(wrapper));
        return ResultVO.okResult();
    }

    @ApiOperation(value = "修改记录")
    @PutMapping("update")
    public ResultVO updateData(@RequestBody FeastRecompense recompense) {
        return ResultVO.okResult(recompenseService.update(recompense, new LambdaQueryWrapper<FeastRecompense>().eq(FeastRecompense::getId, recompense.getId())));
    }

    @ApiOperation(value = "获取单条记录信息")
    @GetMapping("getOne/{id}")
    public ResultVO getOne(@PathVariable String id) {
        return ResultVO.okResult(recompenseService.getById(id));
    }

    @ApiOperation(value = "删除记录")
    @PostMapping("delete")
    public ResultVO delete(@RequestBody List<String> ids) {
        recompenseService.removeByIds(ids);
        return ResultVO.okResult();
    }
}
